package com.admin.education.controllers.api.v1;

import com.admin.education.common.exceptions.BadRequestException;
import com.admin.education.common.jwt.JwtAuthorize;
import com.admin.education.common.jwt.JwtUtils;
import com.admin.education.common.response.IResponseMessage;
import com.admin.education.common.response.ResponseMessage;
import com.admin.education.common.response.ResponseMessageWithData;
import com.admin.education.common.response.Result;
import com.admin.education.dataTransferObject.PageResponse;
import com.admin.education.dataTransferObject.ServicesExcuteResult;
import com.admin.education.dataTransferObject.circles.CirclesTransfer;
import com.admin.education.dataTransferObject.circles.CommentsTransfer;
import com.admin.education.dataTransferObject.circles.Request.CreateCirclesRequest;
import com.admin.education.dataTransferObject.circles.Request.QueryCirclesRequest;
import com.admin.education.dataTransferObject.comments.Request.CreateCommentsRequest;
import com.admin.education.dataTransferObject.userInformation.UserInformationTransfer;
import com.admin.education.services.Interfaces.ICirclesService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.concurrent.Callable;

@RestController
@RequestMapping("/v1/circles")
@JwtAuthorize
@CrossOrigin(origins = "*", maxAge = 3600)
public class CirclesController {
    @Autowired
    private ICirclesService circlesService;

    @PostMapping("")
    public Callable<IResponseMessage> createCircle(@Valid @RequestBody CreateCirclesRequest createCirclesRequest, HttpServletRequest request) {
        return () -> {
            String auth = JwtUtils.getClaims(request, "auth").toString();

            if (null == auth)
                throw new BadRequestException("请先登录");

            UserInformationTransfer userInformationTransfer = JSONObject.parseObject(auth, UserInformationTransfer.class);
            IResponseMessage responseMessage = null;

            ServicesExcuteResult<UserInformationTransfer> result = circlesService.createCircle(userInformationTransfer.getId(), createCirclesRequest);

            if (result.isSuccess()) {
                responseMessage = new ResponseMessageWithData<UserInformationTransfer>(Result.Success, result.getData());
            } else {
                throw new BadRequestException(result.getErrorMessage());
            }
            return responseMessage;
        };
    }

    @GetMapping("")
    public Callable<IResponseMessage> queryCircles(@Valid QueryCirclesRequest circlesRequest) {
        return () -> {
            IResponseMessage responseMessage = null;

            ServicesExcuteResult<PageResponse<CirclesTransfer>> result = this.circlesService.queryCircles(circlesRequest);
            if (result.isSuccess()) {
                responseMessage = new ResponseMessageWithData<PageResponse<CirclesTransfer>>(Result.Success, result.getData());
            } else {
                throw new BadRequestException(result.getErrorMessage());
            }
            return responseMessage;
        };
    }
    @GetMapping("{id}")
    public Callable<IResponseMessage> queryArticle(@PathVariable Long id){
        return ()->{
            IResponseMessage responseMessage = null;

            ServicesExcuteResult<CirclesTransfer> result =this.circlesService.queryCircles(id);
            if(result.isSuccess()){
                responseMessage = new ResponseMessageWithData<CirclesTransfer>(Result.Success,result.getData());
            }
            else{
                throw new BadRequestException(result.getErrorMessage());
            }
            return responseMessage;
        };
    }
    @PostMapping("{id}/admirer")
    public Callable<IResponseMessage> createAdmirer(@PathVariable Long id,HttpServletRequest request){
        return ()->{
            IResponseMessage responseMessage = null;

            String auth = JwtUtils.getClaims(request,"auth").toString();

            if(null==auth)
                throw new BadRequestException("请先登录");

            UserInformationTransfer userInformationTransfer = JSONObject.parseObject(auth,UserInformationTransfer.class);
            ServicesExcuteResult result = this.circlesService.createAdmirer(id,userInformationTransfer.getId());
            if(result.isSuccess()){
                responseMessage = new ResponseMessage(Result.Success);
            }
            else
                throw  new BadRequestException(result.getErrorMessage());

            return  responseMessage;
        };
    }
    @PostMapping("{id}/comments")
    public Callable<IResponseMessage> createComments(@PathVariable Long id, HttpServletRequest request, @RequestBody @Valid CreateCommentsRequest createCommentsRequest){
        return ()->{
            IResponseMessage responseMessage = null;

            String auth = JwtUtils.getClaims(request,"auth").toString();

            if(null==auth)
                throw new BadRequestException("请先登录");

            UserInformationTransfer userInformationTransfer = JSONObject.parseObject(auth,UserInformationTransfer.class);
            ServicesExcuteResult result = this.circlesService.createComments(id,userInformationTransfer.getId(),createCommentsRequest);
            if(result.isSuccess()){
                responseMessage = new ResponseMessage(Result.Success);
            }
            else
                throw  new BadRequestException(result.getErrorMessage());

            return  responseMessage;
        };
    }
    @GetMapping("{id}/comments/")
    public Callable<IResponseMessage> queryComments(@PathVariable Long id){
        return ()->{
            IResponseMessage responseMessage = null;
            ServicesExcuteResult<List<CommentsTransfer>> result = this.circlesService.queryComments(id);
            if(result.isSuccess()){
                responseMessage = new ResponseMessageWithData<List<CommentsTransfer>> (Result.Success,result.getData());
            }
            else
                throw  new BadRequestException(result.getErrorMessage());

            return  responseMessage;
        };
    }
}
